<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoComment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoRating.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoUpdate.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoProvider.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoItem.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/comment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/rating.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/sendMail.php';

class CommentBus {
	private $_commentDao;
	private $_ratingDao;
	private $_updateDao;
	private $_itemDao;
	private $_providerDao;
	
	private $_userBus;
	
	public function __construct() {
		$this->_commentDao = new CommentDao();
		$this->_ratingDao = new RatingDao();
		$this->_updateDao = new UpdateDao();
		$this->_itemDao = new ItemDao();
		$this->_providerDao = new ProviderDao();
		
		$this->_userBus = new UserBus();
	}
	
	public function addComment($comment) {
		global $ubi;
		
		$currentTime = $_SERVER['REQUEST_TIME'];
		$comment->setCreatedDate($currentTime);
		$id = $this->_commentDao->addComment($comment);
		if ($id) {
			// insert update
			$update = new UpdateModel();
			$update->setIndicteeID($comment->getCommentID());
			$update->setModifiedDate($currentTime);
			$update->setUpdateType($ubi['UPDATE']['POST_COMMENT']);
			$update->setUserID($comment->getUserID());
			
			if ($comment->getInteractType() == $ubi['PROVIDER']['VALUE']) {
				$update->setLink('index.php?view&type=provider&id=' . $comment->getIndicteeID());
				$post = $this->_providerDao->getProvider($comment->getIndicteeID());
			} else {
				$post = $this->_itemDao->getItem($comment->getIndicteeID());
				$update->setLink('index.php?view&type=item&id=' . $comment->getIndicteeID());
			}
			$update->setName($comment->getMessage());
			
			$this->_updateDao->addUpdate($update);
			
			// add scrore for user posting a comment
			global $ubi_score;
			$this->_userBus->addScore($comment->getUserID(), $ubi_score['COMMENT']);
			
			// send email
			$postedUser = $this->_userBus->getUser($post->getUserID());
			if (!in_array($ubi['EMAIL']['COMMENT_ON_MY_POST'], $postedUser->getEmailPrivacies())) {
				$commentedUser = $this->_userBus->getUser($comment->getUserID());
				
				if ($comment->getInteractType() == $ubi['PROVIDER']['VALUE']) {
					$post = $this->_providerDao->getProvider($comment->getIndicteeID());
					$urlPost = '<a href="' . 'index.php?view&type=provider&id=' . $post->getProviderID() . '">' . $post->getProvider() . '</a>';
				} else {
					$post = $this->_itemDao->getItem($comment->getIndicteeID());
					$urlPost = '<a href="' . 'index.php?view&type=item&id=' . $post->getItemID() . '">' . $post->getItem() . '</a>';
				}
				
				SendMail::getEmailTemplate($ubi['EMAIL']['COMMENT_ON_MY_POST'], $template, $subject);
				// TODO [HDR] : email sent to users, so give them the full address of the site
				$arrParam = array();
				$arrParam[] = $postedUser->getFirstName() . ' ' . $postedUser->getLastName();
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $commentedUser->getUserID() . '">' . $commentedUser->getUserName() . '</a>';
				$arrParam[] = $urlPost;
				$arrParam[] = $comment->getMessage();
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				
				SendMail::sendEMail($template, $subject, $arrParam, $postedUser->getEmail());
			}
		}
		
		return $id;
	}	
	
	public function getAllComments($indicteeID, $interactType) {
		return $this->_commentDao->getComments($indicteeID, $interactType);
	}
	
	public function getUsersOfComments($comments) {
		$users = array();
				
		foreach ($comments as $com) {
			$users[] = $this->_userBus->getUser($com->getUserID());
		}
		
		return $users;
	}
	
	public function getCommentsOfPage($indicteeID, $interactType, $page) {
		global $ubi;
		
		$from = ($page - 1) * $ubi['COMMENT']['COMMENTS_PER_PAGE'];
		$to = $from +  $ubi['COMMENT']['COMMENTS_PER_PAGE'];
		
		return $this->_commentDao->getComments($indicteeID, $interactType, $from, $to);
	}
	
	public function getNumberOfComments($indicteeID, $interactType) {
		return $this->_commentDao->getNumberOfComments($indicteeID, $interactType);
	}
	
	public function plus($commentID, $userID) {
		global $ubi;
		
		$rating = new RatingModel();
		$rating->setCreatedDate($_SERVER['REQUEST_TIME']); 
		$rating->setIndicteeID($commentID);
		$rating->setInteractType($ubi['COMMENT']['VALUE']);
		$rating->setRatingValue($ubi['RATING']['PLUS']);
		$rating->setUserID($userID);
		
		$ck = $this->_ratingDao->doRateComment($rating);
		if ($ck) {
			// add update
			$comment = $this->getCommentByID($commentID);
			
			$update = new UpdateModel();
			$update->setIndicteeID($commentID);
			$update->setModifiedDate($_SERVER['REQUEST_TIME']);
			$update->setUpdateType($ubi['UPDATE']['PLUS_COMMENT']);
			$update->setUserID($userID);
			
			if ($comment->getInteractType()== $ubi['PROVIDER']['VALUE']) {
				$update->setLink('index.php?view&type=provider&id=' . $comment->getIndicteeID());
			} else {
				$update->setLink('index.php?view&type=item&id=' . $comment->getIndicteeID());
			}
			
			$update->setName($comment->getMessage());
			$this->_updateDao->addUpdate($update);
			
			// send email for its owner
			$ownerComment = $this->_userBus->getUser($comment->getUserID());
			if (!in_array($ubi['EMAIL']['PLUS_MY_COMMENT'], $ownerComment->getEmailPrivacies())) {
				$userPlusComment = $this->_userBus->getUser($userID);
				if ($comment->getInteractType() == $ubi['PROVIDER']['VALUE']) {
					$post = $this->_providerDao->getProvider($comment->getIndicteeID());
					$urlPost = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=provider&id=' . $post->getProviderID() . '">' . $post->getProvider() . '</a>';
				} else {
					$post = $this->_itemDao->getItem($comment->getIndicteeID());
					$urlPost = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=item&id=' . $post->getItemID() . '">' . $post->getItem() . '</a>';
				}
				
				SendMail::getEmailTemplate($ubi['EMAIL']['PLUS_MY_COMMENT'], $template, $subject);
				
				$arrParam = array();
				$arrParam[] = $ownerComment->getFirstName() . ' ' . $ownerComment->getLastName();	
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $userPlusComment->getUserID() . '">' . $userPlusComment->getUserName() . '</a>';
				$arrParam[] = $comment->getMessage();
				$arrParam[] = $urlPost;
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				
				SendMail::sendEMail($template, $subject, $arrParam, $ownerComment->getEmail());
			}
			
			// add score for user plus a comment
			global $ubi_score;
			$this->_userBus->addScore($rating->getUserID(), $ubi_score['LIKE_UNLIKE_COMMENT']);	
		}
		
		return $ck;		
	}
	
	public function minus($commentID, $userID) {
		global $ubi;
		
		$rating = new RatingModel();
		$rating->setCreatedDate($_SERVER['REQUEST_TIME']); 
		$rating->setIndicteeID($commentID);
		$rating->setInteractType($ubi['COMMENT']['VALUE']);
		$rating->setRatingValue($ubi['RATING']['MINUS']);
		$rating->setUserID($userID);
		
		$ck = $this->_ratingDao->doRateComment($rating);
		if ($ck) {
			// add update
			$comment = $this->getCommentByID($commentID);
			
			$update = new UpdateModel();
			$update->setIndicteeID($commentID);
			$update->setModifiedDate($_SERVER['REQUEST_TIME']);
			$update->setUpdateType($ubi['UPDATE']['MINUS_COMMENT']);
			$update->setUserID($userID);
			
			if ($comment->getInteractType()== $ubi['PROVIDER']['VALUE']) {
				$update->setLink('index.php?view&type=provider&id=' . $comment->getIndicteeID());
			} else {
				$update->setLink('index.php?view&type=item&id=' . $comment->getIndicteeID());
			}
			
			$update->setName($comment->getMessage());
			$this->_updateDao->addUpdate($update);
			
			// send email for its owner
			$ownerComment = $this->_userBus->getUser($comment->getUserID());
			if (!in_array($ubi['EMAIL']['MINUS_MY_COMMENT'], $ownerComment->getEmailPrivacies())) {
				$userPlusComment = $this->_userBus->getUser($userID);
				if ($comment->getInteractType() == $ubi['PROVIDER']['VALUE']) {
					$post = $this->_providerDao->getProvider($comment->getIndicteeID());
					$urlPost = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=provider&id=' . $post->getProviderID() . '">' . $post->getProvider() . '</a>';
				} else {
					$post = $this->_itemDao->getItem($comment->getIndicteeID());
					$urlPost = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=item&id=' . $post->getItemID() . '">' . $post->getItem() . '</a>';
				}
				
				SendMail::getEmailTemplate($ubi['EMAIL']['MINUS_MY_COMMENT'], $template, $subject);
				
				$arrParam = array();
				$arrParam[] = $ownerComment->getFirstName() . ' ' . $ownerComment->getLastName();	
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $userPlusComment->getUserID() . '">' . $userPlusComment->getUserName() . '</a>';
				$arrParam[] = $comment->getMessage();
				$arrParam[] = $urlPost;
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				
				SendMail::sendEMail($template, $subject, $arrParam, $ownerComment->getEmail());
			}
			
			global $ubi_score;
			$this->_userBus->addScore($rating->getUserID(), $ubi_score['LIKE_UNLIKE_COMMENT']);	
		}
		
		return $ck;			
	}
	
	public function getCommentByID($commentID) {
		return $this->_commentDao->getCommentByID($commentID);
	}
	
	public function getRatingValues($comments, $userID) {
		global $ubi;
		
		$ratingValues = array();
		foreach ($comments as $com) {
			if ($userID != null) {
				$rating = $this->_ratingDao->getRating($userID, $com->getCommentID(), $ubi['COMMENT']['VALUE']);
				if ($rating) {					
					$ratingValues[$com->getCommentID()] = $rating->getRatingValue();
				} else {
					$ratingValues[$com->getCommentID()] = 0;
				}		
			} else {
				$ratingValues[$com->getCommentID()] = 0;
			}
		}
			
		return $ratingValues;
	}
}
?>